﻿@if (false) {<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.7.min.js" type="text/javascript" />}
@{string list = "list3", pager = "pager3", entity = "SemifinishedDepository";}
<table id="@list" class="scroll" cellpadding="0" cellspacing="0">
</table>
<div id="@pager" class="scroll" style="text-align: center;">
</div>

<script type="text/javascript">
    jQuery(function () {
        $("#@list").jqGrid({
            url: "/@entity/index/",
            colNames: ["物料编号", "物料名称", "规格","型号", "单位", "库存总量", "安全库存", "备注"],
            colModel: [
            { name: 'ProductValueObject.Number', index: 'ProductValueObject.Number', width: 80, search: false },
            $.extend($.jgridx.strCol("ProductValueObject.Name"),{ editable : false }),
            { name: 'ProductValueObject.Spec', index: 'ProductValueObject.Spec', width: 80, search: false },
            { name: 'ProductValueObject.Model', index: 'ProductValueObject.Model', width: 80, search: false },
            $.extend(true, $.jgridx.strCol("ProductValueObject.Unit"), { search: false }),
            { name: 'Total', index: 'Total', width: 80, search: false },
            $.extend(true, $.jgridx.intNullCol("SafetyCount"), { search: false }),
            {
                name: "Remark",
                index: "Remark",
                width: 280,
                editable: true,
                edittype: "textarea",
                search: false
            }],

            pager: $('#@pager'),
            sortname: "DepositoryId",
            sortorder: "desc",
            subGrid: true,

            subGridRowExpanded: function (subgrid_id, row_id) {
                var subgrid_table_id,
                pager_id;
                subgrid_table_id = subgrid_id + "_t";
                pager_id = "p_" + subgrid_table_id;
                $("#" + subgrid_id).html("<table id='" + subgrid_table_id + "' class='scroll'></table><div id='" + pager_id + "' class='scroll'></div>");
                jQuery("#" + subgrid_table_id).jqGrid({
                    url: "/DepositoryItem/Index?depositoryId=" + row_id,
                    datatype: "json",
                    colNames: ["批次号", "数量", "存放位置"],
                    colModel: [
                    $.extend($.jgridx.strCol("Number"), { editable : false }),
                    $.jgridx.intCol("Count"),
                    $.jgridx.strNullCol("Location")
                    ],
                    rowNum: 10,
                    pager: pager_id,
                    sortname: 'DepositoryItemId',
                    sortorder: "desc",
                    height: '100%'
                });
                jQuery("#" + subgrid_table_id).jqGrid('navGrid', "#" + pager_id,
                {
                    @if(Roles.IsUserInRole("depositorymanagers")){
                    @:edit: true,
                    @:del: true,
                    }else{
                        @:edit: false,
                        @:del: false,
                    }
                    add: false
                },
                {
                    url: "/DepositoryItem/Edit"
                },
                {
                    url: "/DepositoryItem/Add?depositoryId=" + row_id
                },
                {
                    url: "/DepositoryItem/Delete/"
                }, {}, {});
            },
            loadComplete: function (data) {

                if (!data.success)
                    $(this).jqGrid("clearGridData", true);
            }
        });
        $("#@list").jqGrid('navGrid', '#@pager',
        {
        @if(Roles.IsUserInRole("depositorymanagers")){
            @:edit: true,
            @:del: true,
        }else{
            @:edit: false,
            @:del: false,
        }
            add: false,
            search: true,
            refresh: true
        },
        {
            url: "/@entity/edit/"
        },
        {
            url: "/@entity/add/"
        },
        {
            url: "/@entity/delete/"
        }, {}, {}).navSeparatorAdd("@pager", { sepclass: "ui-separator", sepcontent: '' })
        .jqGrid('navButtonAdd', "@pager", { caption: "",
            buttonicon: "ui-icon ui-icon-print",
            position: "last",
            title: "打印库存单",
            onClickButton: function () {
                var rowids = $("#@list").jqGrid('getGridParam', 'selarrrow');
                if (rowids.length == 0) {
                    Alert("警告", "请选择记录！");
                    return;
                }
                var title = "半成品库存单"
                var p = $("#@list");
                var h = p.parent().parent().parent().find("table").eq(0).find("thead");
                var printhtml = []
                printhtml.push(h.html());
                $.each(p.find("tr"), function (index, value) {
                    if (inarray(rowids, $(this).attr("id"))) {
                        printhtml.push("<tr>" + $(this).html() + "</tr>");
                    }
                });

                var printdiv = $('<div id="printdiv"><h1>' + title + '</h1><table cellspacing="0" cellpadding="0" border="0">' + printhtml.join('') + '</table></div>');
                $.each(printdiv.find("tr"), function () {
                    $(this).children().eq(0).remove();
                });
                printdiv.find("span").remove();
                Print(printdiv);
            }
        })
        .jqGrid('navButtonAdd', "@pager", { caption: "",
            buttonicon: "ui-icon ui-icon-disk",
            position: "last",
            title: "导出",
            onClickButton: function () {
                if ($("#@(list)excel").length == 0) {
                    $(this).after('<form id="@(list)excel" method="post" action="/@entity/Report.xls"></form>');
                }
                $("#@(list)excel").submit()
                //$.ajax("/PartsInDepository/Report");
            }
        })
        ;
    });
</script>